@extends('backend.layouts.base')
@section('title', '部门管理')
@section('content')
    <div class="all">
        <div id="app" v-cloak>
            {{--<div class="vue-head">
                <div class="vue-main-title" style="margin-bottom:20px">
                    <div class="vue-main-title-left"></div>
                    <div class="vue-main-title-content">部门管理</div>
                    <div class="vue-main-title-button">
                        <el-button type="primary" plain icon="el-icon-plus" size="small" @click="openAddDepartment()">添加部门</el-button>
                    </div>
                </div>
                <div class="vue-search">
                    <el-form :inline="true" class="demo-form-inline">
                        <el-input  v-model="search_text" style="width:15%;margin-left:16px;margin-right:16px;" placeholder="请输入部门名称">
                        </el-input>
                        <el-form-item label="">
                            <el-button type="primary" @click="onSearch">搜索</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </div>--}}
            <div class="vue-main">
                <div class="vue-main-form">
                    <div class="vue-main-title" style="margin-bottom:20px">
                        <div class="vue-main-title-left"></div>
                        <div class="vue-main-title-content" style="flex:0 0 130px">部门列表</div>
                        <div class="vue-main-title-button">
                            <el-button type="primary" plain icon="el-icon-plus" size="small" @click="openAddDepartment()">添加部门</el-button>
                        </div>
                    </div>
                    <el-table
                            :data="list"
                            style="width: 100%"
                            row-key="id"
                            ref="list"
                            :tree-props="{ children: 'sub_departments' }"
                    >
                        <el-table-column type="" fixed prop="id" label="ID" width="120">
                        </el-table-column>
                        <el-table-column type="" prop="display_order" label="排序" width="130">
                        </el-table-column>
                        <el-table-column prop="name" label="部门名称" width="200">
                        </el-table-column>
                        <el-table-column prop="parent_name" label="上级部门" width="200">
                        </el-table-column>
                        <el-table-column
                                prop="user_count"
                                label="员工数量"
                                width="200"
                                align="center"
                        >
                        </el-table-column>
                        <el-table-column label="操作" width="200">
                            <template slot-scope="scope">
                                <div class="table-option">
                                    <el-link title="添加子部门"  v-if="scope.row.createDepartment != 0"
                                             :underline="false" @click="addChild(scope.row)" style="text-align: center;width:70px">
                                        <a>添加子部门</a>
                                    </el-link>
                                    <el-link title="编辑" :underline="false" @click="edit(scope.row)" style="text-align: center;width:30px">
                                        <a>编辑</a>
                                    </el-link>
                                    <el-link title="删除" :underline="false" @click="deleteData(scope.row.id)" style="text-align: center;width:30px">
                                        <a>删除</a>
                                    </el-link>
                                </div>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </div>
            <el-dialog
                    title="添加部门"
                    :visible.sync="addDepartmentShow"
                    width="40%"
                    center
            >
                <el-form
                        ref="add_department_form"
                        :model="add_department_form"
                        :rules="rules"
                        label-width="100px"
                >
                    <el-form-item label="部门名称" prop="name">
                        <el-input
                                v-model="add_department_form.name"
                                placeholder=""
                                style="width:70%;"
                        ></el-input>
                    </el-form-item>
                    <el-form-item label="排序" prop="display_order">
                        <el-input
                                v-model="add_department_form.display_order"
                                placeholder=""
                                style="width:70%;"
                        ></el-input>
                    </el-form-item>
                    <el-form-item
                            label="所属部门"
                            v-if="!add_parent_department_item.id"
                    >
                        <el-cascader ref="depCascader"
                                     style="width:80%;" @change="changeDepartment"
                                     v-model="add_department_form.parent_arr"
                                     :options="list"
                                     :props="{
              checkStrictly: true,
              label: 'name',
              value: 'id',
              children: 'has_one_sub_department',
              disabled: 'create_department_disabled'
            }"
                                     clearable
                        ></el-cascader>
                    </el-form-item>
                    <el-form-item label="所属部门" v-if="add_parent_department_item.id">
                        <el-input
                                v-model="add_parent_department_item.name"
                                :disabled="true"
                                placeholder=""
                                style="width:70%;"
                        ></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button  @click="addDepartmentShow = false">取 消</el-button>
                    <el-button type="primary" @click="checkForm('add_department_form')" :disabled="saving">保存</el-button>
                </span>
            </el-dialog>

            <el-dialog
                    title="修改名称"
                    :visible.sync="editDepartmentShow"
                    width="40%"
                    center
            >
                <el-form
                        ref="edit_department_form"
                        :rules="rules"
                        :model="edit_department_form"
                        label-width="100px"
                >
                    <el-form-item label="部门名称" prop="name">
                        <el-input
                                v-model="edit_department_form.name"
                                placeholder=""
                                style="width:70%;"
                        ></el-input>
                    </el-form-item>
                    <el-form-item label="排序" prop="display_order">
                        <el-input
                                type="number"
                                v-model="edit_department_form.display_order"
                                placeholder=""
                                style="width:70%;"
                        ></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button  @click="editDepartmentShow = false">取 消</el-button>
                    <el-button type="primary" @click="checkForm('edit_department_form')" :disabled="saving">保存</el-button>
                </span>
            </el-dialog>
        </div>
    </div>

    <script>
        let _default_department_list = [];
        let _search_department_list = [];
        let _search_text = "";
        var app = new Vue({
            el: "#app",
            delimiters: ['[[', ']]'],
            name: 'article',
            data() {
                return {
                    list:[],
                    search_form:{
                        name:''
                    },
                    search_text:'',
                    add_department_form: {
                        name: "",
                        display_order: "",
                        parent_arr: []
                    },
                    edit_department_form: {
                        name: "",
                        display_order: "",
                        id: ""
                    },
                    edit_department_item: {},
                    addDepartmentShow: false, //添加部门
                    editDepartmentShow: false, //修改部门名称
                    category_list:[],
                    rules: {
                        name: [{ required: true, message: "该项为必填项", trigger: "blur" }],
                        parent_arr: [
                            { required: true, message: "该项为必填项", trigger: "blur" }
                        ]
                    },
                    saving:false,
                    add_parent_department_item: {}
                }
            },
            created() {

            },
            mounted() {
                this.getData();
            },
            methods: {
                changeDepartment(e) {
                    console.log(e)
                },
                getData() {
                    let json = {
                        name:this.search_form.name
                    };
                    let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                    this.$http.post('{!! absolute_admin_url('business.department.getList') !!}',{search:json}).then(function(response) {
                        if (response.data.result) {
                            this.list = response.data.data;
                            _default_department_list = this.list;
                            loading.close();

                        } else {
                            this.$message({
                                message: response.data.msg,
                                type: 'error'
                            });
                        }
                        loading.close();

                    }, function(response) {
                        this.$message({
                            message: response.data.msg,
                            type: 'error'
                        });
                        loading.close();
                    });
                },

                search() {
                    this.getData();
                },
                openAddDepartment() {
                    this.add_parent_department_item = {};
                    this.addDepartmentShow = true;
                    this.saving = false;
                    this.add_department_form = {
                        name: "",
                        display_order: "",
                        parent_arr: []
                    }
                },
                checkForm(formName) {
                    this.$refs[formName].validate(valid => {
                        if (valid) {
                            if (formName == "edit_department_form") {
                                this.editDepartmentData();
                            } else if (formName == "add_department_form") {
                                this.addDepartmentData();
                            }
                        } else {
                            return false;
                        }
                    });
                },
                edit(obj) {
                    this.edit_department_item = obj;
                    this.edit_department_form.name = obj.name;
                    this.edit_department_form.display_order = obj.display_order;
                    this.edit_department_form.id = obj.id;
                    this.editDepartmentShow = true;
                    this.saving = false;
                },
                addChild(obj) {
                    console.log(obj);
                    this.add_department_form = {
                        name: "",
                        display_order: "",
                        parent_arr: []
                    }
                    this.add_parent_department_item = obj;
                    this.addDepartmentShow = true;
                    this.saving = false;
                },
                editDepartmentData() {
                    if(this.saving){
                        return;
                    }
                    this.editDepartmentShow = false;
                    this.saving = true;
                    let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                    this.$http.post('{!! absolute_admin_url("business.department.update") !!}',{params:this.edit_department_form})
                        .then(function (response) {
                                if (response.data.result) {
                                    this.$message({type: 'success',message: '操作成功!'});
                                    this.getData();
                                }
                                else{
                                    this.$message({type: 'error',message: response.data.msg});
                                }
                                loading.close();
                                setTimeout(function () {
                                    this.saving = false;
                                },500)
                            },function (response) {
                                this.$message({type: 'error',message: response.data.msg});
                                loading.close();
                                this.saving = false;
                            }
                        );
                },
                addDepartmentData() {
                    if(this.saving){
                        return;
                    }
                    this.addDepartmentShow = false;
                    let obj = {};
                    obj.name = this.add_department_form.name;
                    obj.display_order = this.add_department_form.display_order;
                    if (
                        this.add_parent_department_item &&
                        this.add_parent_department_item.id
                    ) {
                        obj.pid = this.add_parent_department_item.id;
                    } else {
                        obj.pid = this.add_department_form.parent_arr[
                        this.add_department_form.parent_arr.length - 1
                            ];
                    }
                    this.saving = true;
                    console.log(obj);
                    let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                    this.$http.post('{!! absolute_admin_url("business.department.create") !!}',{params:obj})
                        .then(function (response) {
                                if (response.data.result) {
                                    this.$message({type: 'success',message: '操作成功!'});
                                    this.getData();
                                }
                                else{
                                    this.$message({type: 'error',message: response.data.msg});
                                }
                                loading.close();
                                setTimeout(function () {
                                    this.saving = false;
                                },500)
                            },function (response) {
                                this.$message({type: 'error',message: response.data.msg});
                                loading.close();
                                this.saving = false;
                            }
                        );
                },

                deleteData(id) {
                    this.$confirm('确定删除这条数据吗', '提示', {confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning'}).then(() => {
                        let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                        this.$http.post('{!! absolute_admin_url('business.department.delete') !!}',{id:id}).then(function (response) {
                                if (response.data.result) {
                                    this.$message({type: 'success',message: '操作成功!'});
                                    this.getData();
                                }
                                else{
                                    this.$message({type: 'error',message: response.data.msg});
                                }
                                loading.close();
                            },function (response) {
                                this.$message({type: 'error',message: response.data.msg});
                                loading.close();
                            }
                        );
                    }).catch(() => {
                        this.$message({type: 'info',message: '已取消操作'});
                    });
                },
                onSearch() {
                    //不能请求后端，请求后端样式有问题
                    if (this.search_text == _search_text){
                        return;
                    }
                    _search_text = this.search_text;
                    this.list = [];
                    this.$nextTick(() => {
                        if (_search_text == "") {
                            this.list = _default_department_list;
                        } else {
                            _search_department_list = [];
                            searchDepartment();

                            this.list = _search_department_list;
                            this.$refs.list.doLayout();
                            console.log(this.list, _search_department_list);
                        }
                    });
                }

            },
        })
        function searchDepartment(data = _default_department_list) {
            for (let i = 0; i < data.length; i++) {
                if (data[i].name.indexOf(_search_text) >= 0) {
                    _search_department_list.push(data[i]);
                }
                if (data[i].sub_departments && data[i].sub_departments.length > 0) {
                    searchDepartment(data[i].sub_departments);
                }
            }
        }

    </script>
@endsection
